﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace WebChamCong
{
    public partial class manUser : System.Web.UI.Page
    {
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadrole();

            }
        }
        protected void loadrole()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("ID", System.Type.GetType("System.String"));
            tb.Columns.Add("Họ Tên Nhân Viên", System.Type.GetType("System.String"));
            tb.Columns.Add("Quyền Hạn", System.Type.GetType("System.String"));
            List<DTO.UserRoleDTO> listRoles = BUS.UserRolesBUS.getUserRole();
            
            foreach (var objrole in listRoles)
            {
                string strman = "";
                DataRow dr = tb.NewRow();
                DTO.NhanVienDTO nv = BUS.NhanVienBUS.GetNhanViensByID(objrole.UserRole);
                if (nv == null)
                {
                    dr["Họ Tên Nhân Viên"] = Properties.Settings.Default.Admin;
                    dr["ID"] = -2;
                }
                else
                {
                    dr["ID"] = nv.UserId;
                    dr["Họ Tên Nhân Viên"] = nv.FullName; 
                }

                string str1 = objrole.Action;
                string[] str = str1.Split('&');
                for (int i = 0; i < str.Count(); i++)
                {
                    DTO.RoleDTO kq = BUS.RolesBUS.getRoleByID(int.Parse(str[i]));
                    if (strman != "")
                    {
                        strman += "," + kq.RoleDescription;
                    }
                    else
                    {
                        strman += kq.RoleDescription;
                    }
                }
                dr["Quyền Hạn"] = strman;
                tb.Rows.Add(dr);
            }
           
            tb.AcceptChanges();
            gv_listAccount.DataKeyNames = new string[] {"ID"};
            gv_listAccount.DataSource = tb;
            gv_listAccount.DataBind();
            for (int i = 0; i < gv_listAccount.Rows.Count; i++)
            {
                GridViewRow row = gv_listAccount.Rows[i];
                ((CheckBox)row.FindControl("myCheckBox")).Checked = true;
            }
        }

        protected void gv_listAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void gv_listAccount_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            

        }

        protected void Btluu_Click(object sender, EventArgs e)
        {
        }

        

        protected void myCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            GridViewRow gr = (GridViewRow)chk.Parent.Parent;
            MessageBox(gv_listAccount.DataKeys[gr.RowIndex].Value.ToString());
        }
    }
}